<template>
  <div class="login">
    <el-form
      :label-position="labelPosition"
      label-width="80px"
      :model="formdata"
      class="login-form"
    >
      <el-form-item label="用户名">
        <el-input v-model="formdata.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="formdata.password"></el-input>
      </el-form-item>
      <el-button type="primary" class="login-button" @click="handeLogin">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labelPosition: "top",
      formdata: {
        username: "admin",
        password: "123456"
      }
    };
  },
  created() {
    sessionStorage.removeItem("token");
  },
  methods: {
    async handeLogin() {
      const res = await this.$axios.post("login", this.formdata);
      const {
        data,
        meta: { msg, status }
      } = res.data;
      if (status === 200) {
        sessionStorage.setItem("token", data.token);
        this.$message({
          message: msg,
          type: "success",
          duration: 1000
        });
        this.$router.push({ name: "home" });
      } else {
        this.$message({
          message: msg,
          type: "error",
          duration: 1000
        });
      }
    }
  }
};
</script>
<style lang="scss">
.login {
  height: 100%;
  background-color: #324152;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    width: 400px;
    padding: 30px;
    background-color: white;
    border-radius: 5px;
  }

  .login-button {
    width: 100%;
  }
}
</style>
